<?php

  $DB_link = null;
  $DB_base = null;
  
  function db_connect($confpath) {
    include $confpath;
    
    $DB_link = mysql_connect($DB_host, $DB_user, $DB_pass);
    if (!$DB_link) {
      die ('Non riesco a connettermi: ' . mysql_error());
    }
    
    $DB_base = mysql_select_db($DB_name, $DB_link);
    if (!$DB_base) {
      die ("Errore nella selezione del database $DB_name: " . mysql_error());
    }
    return true;
  }
  
  function db_query($query) {
    
    $result = mysql_query($query);
    
    if (!$result) {
      die("Errore nella query $query: " . mysql_error());
    }
    
    return $result;
  }
  
  function db_rconnect_n_query($query) {
    include '../dbconf.php';
    
    $DB_link = mysql_connect($DB_host, $DB_user, $DB_pass);
    if (!$DB_link) {
      die ('Non riesco a connettermi: ' . mysql_error());
    }
    
  	$sql = explode(';', $query);
	foreach ($sql as $key => $val) {
		if (strlen(trim($val," \t")) > 2) {
	    	$result = mysql_query($val);
	    	if (!$result) {
      			die("Errore nella query ". $val. ": " . mysql_error());
    		}
		}
	}
    //$result = mysql_query($query);
    
    if (!$result) {
      die("Errore nella query $query: " . mysql_error());
    }
    
    return $result;
  }
  
  function db_query_array($query) {
    
    $result = mysql_query($query);
    
    if (!$result) {
      die("Errore nella query $query: " . mysql_error());
    }
    
  	$record = mysql_fetch_array($result);
    return $record;
  }
  
?>